﻿@using LanDBom.Models
@using LanDBom.Resources
@{
    ViewBag.Title = "JJInksm";
    Layout = "~/Views/Shared/_Genertals.cshtml";
}
@*目标：分人员显示 采购人员显示外购物料 ，销售人员显示自制物料，*@
<from>
    <fieldset class="boun">
        <legend></legend>
        <div class="row bian">
            <label class="col-sm-3 breadcrumb-item ">步骤三：BOM工艺添加</label>
            <ul id="lisul" class="row col-sm-5">
                <li class="lisu"></li>&rArr;
                <li class="lisu"></li>&rArr;
                <li class="lisu">3</li>&rArr;
                <li class="lisu"></li>&rArr;
                <li class="lisu"></li>&rArr;
                <li class="lisu"></li>&rArr;
                <li class="lisu"></li>
            </ul>
        </div>
    </fieldset>
</from>
<div id="wid">
    <form class="row padtop col-12">
        <fieldset class="boun">
            <legend class="len">查询条件</legend>
            <div class="row">
                <p class="col">
                    @Html.Label(Resource.String152)
                    @Html.TextBox("shen")
                </p>
                <p class="col">
                    @Html.Label(Resource.String153)
                    @Html.TextBox("ming")
                </p>
                <p class="col">
                    @Html.Label(Resource.String154)
                    @Html.TextBox("shi")
                </p>
                <p class="col">
                    <input type="button" value="@Resource.String155" name="cha" class="btn-secondary btn-sm" />
                </p>
            </div>

        </fieldset>
    </form>
    <div>
        <table class="table tab">
            <thead class="thead-light">
                <tr style="text-align-last:center;">
                    <th scope="col" style="width:70px;">@Resource.String156</th>
                    <th scope="col" >@Resource.String157</th>
                    <th scope="col" style="width:370px;">@Resource.String158</th>
                    <th scope="col">@Resource.String159</th>
                    <th scope="col">@Resource.String160</th>
                    <th scope="col">@Resource.String161</th>
                    <th scope="col" style="width: 150px">@Resource.String162</th>
                    <th scope="col" style="width:70px;"></th>
                </tr>
            </thead>
            <tbody class="gun LMKJN" id="BUtain" style="height:570px;">
                
            </tbody>
        </table>
    </div>
</div>
@section scripts{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        //点击申请单号判断当前步骤进行跳转
        $(function () {
            $("#li4").css({ "color": "black", "background-color": "white" })
            $(document).on("click", "#Dan", function (e) {
                var B_dan = $(e.target).text();
                var B_dount = $(e.target).next().text();
                var B_zhang = $(e.target).parent().children().eq(11).text();
                //2:bom 3:工艺 4:询价 6：报价
               window.location.href = "/Material/Index?names=" + B_dan;
            })
        })
        //查询功能
        $(function () {
            $(document).on("click", "#Shancu", function (ad) {
                if (confirm("是否删除该行")) {
                    var bian = $(ad.target).parent().parent().children().first().next().next().text()
                    $.ajax({
                        url: '/Home/Index',
                        type: 'POST',
                        data: { "AP_bian": bian, "states": 4 },
                        success: function (date) {
                            alert(date)
                            location.reload(true)
                        }, error: function () {
                            alert("系统错误")
                        }
                    })
                }
            })
            skonf()
            $('input[name="cha"]').click(function () {
                skonf()
            })
            function skonf() {
                var shen = $("#shen").val()
                var shi = $("#shi").val()
                var ming = $("#ming").val()
                $.ajax({
                    url: '/Material/JJInksm',
                    type: 'POST',
                    data: { "AP_bian": shen, "App_Kehu": ming, "App_JTime": shi, "states": 1, "renid": $("#Login_name").text()},
                    success: function (date) {
                        createRow(date);
                        console.log(date)
                    },
                    error: function () {
                        alert("错误 请重试！")
                    }
                })
               
            }
            function createRow(date) {
                $("#BUtain tr").remove();
                $.each(date, function (i, item) {
                    var inde1 = 0
                    var inde2 = 0
                    var BINA = 0
                    var $tr = $(`<tr>
                                <td style="width:70px;"></td>
                                <td style="display:none">${item.App_id}</td>
                                <td id="Dan" style="color:#2a6bcc;">${item.AP_bian}</td>
                                <td class="sr-only C_jin" style="display:none" id="poi">${item.App_bu}</td>
                                <td style="width:370px;">${item.App_Kehu}</td>
                                <td>${item.APP_name}</td>
                                <td>${item.App_qishi}</td>
                                <td>${item.App_JTime}</td>
                                <td style="display:none" id="shun">${item.APP_fenL}</td>
                                <td style="width:150px">
                                    <div style="display:flex">
                                    </div>
                                </td>
                                 <td style="width:70px;"></td>
                                 <td style="display:none" id="zhand">${item.App_zhand}</td>
                            </tr>
                            `);
                    if ($("#Login_bm").text() == "采购") {
                        var BINA = 0
                    } else {
                        var BINA = 1
                    }
                    $.ajax({
                        url: '/Material/JJInksm',
                        type: 'POST',
                        data: { "AP_bian": item.AP_bian, "states": 5, "RENY": BINA },
                        success: function (date) {
                            if (date == "添加") {
                                $.ajax({
                                    url: '/Material/JJInksm',
                                    type: 'POST',
                                    data: { "AP_bian": item.AP_bian, "states": 2 },
                                    success: function (date) {
                                        $.each(date, function (i, item) {
                                            if (item.BOM2_Lei == "外购") {
                                                inde1 += 1
                                            }
                                            else if (item.BOM2_Lei == "自制") {
                                                inde2 += 1
                                            }
                                            console.log(inde1, inde2)
                                            dhj()
                                        })
                                    },
                                    error: function () {
                                        alert("错误 请重试！")
                                    }
                                })
                            }
                        },
                        error: function () {
                            alert("错误 请重试！")
                        }
                    })
                    function dhj() {
                        if (inde1 > 0) {
                            if ($("#Login_sf").text() == "采购主管" || $("#Login_sf").text() == "业务发展部经理") {
                                $("#BUtain").append($tr);
                            }
                            else if ($("#Login_sf").text() == "采购员") {
                                $.ajax({
                                    url: '/Procure/Price_main',
                                    type: 'POST',
                                    data: { "App_bian": item.AP_bian, "id": $("#Login_name").text(), "state": 13 },
                                    success: function (date) {
                                        if (date == "添加") {
                                            $("#BUtain").append($tr);
                                            sknffook() 
                                        }
                                    },
                                    error: function () {
                                        alert("错误 请重试！")
                                    }
                                })
                            }
                        }
                        if (inde2 > 0) {
                            if ($("#Login_sf").text() == "客户经理" || $("#Login_sf").text() == "销售助理" || $("#Login_sf").text() == "业务发展部经理") {
                                $("#BUtain").append($tr);
                            }
                            else if ($("#Login_sf").text() == "销售" && $("#Login_N").text() == item.AP_ren) {
                                $("#BUtain").append($tr);
                            }
                        }
                        sknffook() 
                    }
                })
            }
            function sknffook() {
                //$("#li4").css({ "color": "black", "background-color": "white" })
                var len = $("#BUtain tr").length;
                for (var i = 0; i < len; i++) {
                    //获取当前行的值
                    var shuan = $('#BUtain tr:eq(' + i + ') td:nth-child(4)').text() * 1;
                    var zhang = $('#BUtain tr:eq(' + i + ') td:nth-child(12)').text() * 1;
                    $('#BUtain tr:eq(' + i + ') td:nth-last-child(3) div span').remove();
                    for (var j = 1; j < zhang+1; j++) {
                        var $li = $(`<span style="height:13px;width:13px;border:1px solid deepskyblue;margin: 4px;"></span>`);
                        if (j <= shuan) {
                             $li = $(`<span style="height:13px;width:13px;background-color:deepskyblue;margin: 4px;"></span>   `);
                        }
                        $('#BUtain tr:eq(' + i + ') td:nth-last-child(3) div').append($li);
                    }
                    $('#BUtain tr:eq(' + i + ') td:first').text(i + 1);
                }
            }
        })
    </script>
}
